package puziran.sofa.util;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;

public class MemCachedUtil
{
  private static MemCachedClient cachedClient = new MemCachedClient();

  static
  {
    SockIOPool pool = SockIOPool.getInstance();

    String[] servers = ReadPropertiesFile.getString("config/application", "mips").split(",");

    Integer[] weights = { Integer.valueOf(3) };

    pool.setServers(servers);
    pool.setWeights(weights);

    pool.setInitConn(10);
    pool.setMinConn(10);
    pool.setMaxConn(1000);
    pool.setMaxIdle(3600000L);

    pool.setMaintSleep(60L);

    pool.setNagle(false);
    pool.setSocketTO(60);
    pool.setSocketConnectTO(0);

    pool.initialize();
  }

  public static boolean add(String key, Object value)
  {
    return cachedClient.add(key, value);
  }

  public static boolean add(String key, Object value, Integer expire) {
    return cachedClient.add(key, value, expire);
  }

  public static boolean put(String key, Object value) {
    return cachedClient.set(key, value);
  }

  public static boolean put(String key, Object value, Integer expire) {
    return cachedClient.set(key, value, expire);
  }

  public static boolean replace(String key, Object value) {
    return cachedClient.replace(key, value);
  }

  public static boolean replace(String key, Object value, Integer expire) {
    return cachedClient.replace(key, value, expire);
  }

  public static Object get(String key) {
    return cachedClient.get(key);
  }
  public static boolean exists(String key) {
    return cachedClient.keyExists(key);
  }
}